package com.meiyue.library.custom.controller;

import com.meiyue.bean.base.PageModel;
import com.meiyue.bean.protocol.NetResult;
import com.meiyue.library.custom.service.BaseService;
import com.meiyue.library.utils.ResultUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.Serializable;
import java.util.List;

/**
 * MeiyueJavaProject
 * com.meiyue.library.custom.controller
 * Created by Chris Chen
 * 2017/9/15
 * Explain:通用控制器接口 KV请求方式
 */
public abstract class BaseKvController<T, I extends Serializable, S extends BaseService> {

    public abstract S getService();//获得Service 处理不好自动装载的笨办法

    //1.获取一个数据
    @RequestMapping("/getOne")
    public NetResult<T> getOne(I id) {
        return ResultUtils.buildResult((T) (getService().getOne(id)));
    }

    //2.获取数据列表
    @RequestMapping("/getList")
    public NetResult<List<T>> getList() {
        return ResultUtils.buildResult(getService().getList());
    }

    //3.获取分页
    @RequestMapping("/getPage")
    public NetResult<PageModel<T>> getPage(int page, int pageSize) {
        return ResultUtils.buildResult(getService().getPage(page, pageSize));
    }

    //4.删除一条
    @RequestMapping("/removeOne")
    public NetResult<Boolean> removeOne(I id) {
        return ResultUtils.buildResult(getService().removeOne(id));
    }

    //5.删除一批
    @RequestMapping("/removeList")
    public NetResult<Boolean> removeList(String ids) {
        return ResultUtils.buildResult(getService().removeList(ids));
    }

    //6.添加一条数据
    @RequestMapping("/addOne")
    public NetResult<Boolean> addOne(T t) {
        return ResultUtils.buildResult(getService().addOne(t));
    }

    //6.修改一条数据
    @RequestMapping("/updateOne")
    public NetResult<Boolean> updateOne(T t) {
        return ResultUtils.buildResult(getService().updateOne(t));
    }

}
